const {
	exec,
	escape
} = require('../db/mysql');


function getList(author, keyword) {
	let sql = `select * from blogs where 1=1 `
	if (author) {
		author = escape(author)
		sql += `and author = ${author} `
	}
	if (keyword) {
		sql += `and title like '%${keyword}%' `
	}
	sql += `order by id desc`
	return exec(sql)
}

function getDetail(id) {
	if (id) {
		id = escape(id)
		let sql = `select * from blogs where id = ${id}`
		return exec(sql)
	}
}

function del(id,author){
	id = escape(id)
	author = escape(author)
	let sql = `delete from blogs where id = ${id} and author = ${author};`
	return exec(sql)
}

function newBlog(newBlogData={}){
	let title = escape(newBlogData.title);
	let content = escape(newBlogData.content);
	let author = escape(newBlogData.author);
	let createTime = +new Date();
	let sql = `insert into blogs (title,content,author,createTime) values (${title},${content},${author},${createTime});`;
	return exec(sql);
}


function upDateBlog(){
	
}
module.exports = {
	getList,
	getDetail,
	del,
	newBlog,
	upDateBlog
}
